package com.pxst.agent.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pxst.agent.entity.SysAgentCommissionSettings;
import com.pxst.agent.entity.SysAgents;
import com.pxst.agent.entity.SysRebateScheme;
import com.pxst.agent.mapper.SysAgentCommissionSettingsMapper;
import com.pxst.agent.mapper.SysAgentsMapper;
import com.pxst.agent.mapper.SysRebateSchemeMapper;
import com.pxst.agent.req.RateSettingCreateReq;
import com.pxst.agent.req.RateSettingReq;
import com.pxst.agent.resp.RateSettingResp;
import com.pxst.agent.service.SysAgentCommissionSettingsService;
import com.pxst.merchant.entity.SysMerchantInfo;
import com.pxst.resp.ApiResp;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author zarke
* @since 2023-09-25 10:21:01
*/
@Service
@Transactional
@Slf4j
public class SysAgentCommissionSettingsServiceImpl extends JoinServiceImpl<SysAgentCommissionSettingsMapper, SysAgentCommissionSettings> implements SysAgentCommissionSettingsService {

    @Autowired
    private SysAgentCommissionSettingsMapper sysAgentCommissionSettingsMapper;

    @Autowired
    private SysAgentsMapper sysAgentsMapper;

    @Autowired
    private SysRebateSchemeMapper sysRebateSchemeMapper;

    /**
     * 搜索
     * @param rateSettingReq
     * @return
     */
    @Override
    public ApiResp<Page<RateSettingResp>> getCommission(RateSettingReq rateSettingReq) {
        JoinLambdaWrapper<SysAgentCommissionSettings> jlw = new JoinLambdaWrapper<>(SysAgentCommissionSettings.class);


        jlw.eq(StringUtils.hasText(rateSettingReq.getCurrency()),SysAgentCommissionSettings::getCurrency,rateSettingReq.getCurrency())
                .eq(rateSettingReq.getYear() != null ,SysAgentCommissionSettings::getYear,rateSettingReq.getYear())
                .eq(rateSettingReq.getMonth() != null ,SysAgentCommissionSettings::getMonth,rateSettingReq.getMonth())
                .in(rateSettingReq.getMerchantId() != null && rateSettingReq.getMerchantId().size() > 0 ,
                        SysAgentCommissionSettings::getMerchantId,rateSettingReq.getMerchantId())
                .eq(rateSettingReq.getAgentId() != null ,SysAgentCommissionSettings::getAgentId,rateSettingReq.getAgentId());

        jlw.leftJoin(SysMerchantInfo.class,SysMerchantInfo::getId,SysAgentCommissionSettings::getMerchantId)
                .selectAs((cb) -> {
                    cb.addFunAlias(SysMerchantInfo::getCode,RateSettingResp::getMerchantCode)
                        .addFunAlias(SysMerchantInfo::getName,RateSettingResp::getMerchantName)
                            .add(SysMerchantInfo::getFiBankRate)
                            .add(SysMerchantInfo::getFiQrRate)
                            .add(SysMerchantInfo::getFoRate)
                            .add(SysMerchantInfo::getFiTwRate);
                })
                .end();

        jlw.leftJoin(SysAgents.class,SysAgents::getId,SysAgentCommissionSettings::getAgentId,"a")
                .selectAs((cb) -> {
                    cb.addFunAlias(SysAgents::getFullName,RateSettingResp::getAgentName)
                        .addFunAlias(SysAgents::getDisplayId,RateSettingResp::getDisplayId);
                })
                .eq(rateSettingReq.getBelongId() != null,SysAgents::getBelongId,rateSettingReq.getBelongId())
                .or()
                .eq(rateSettingReq.getBelongId() != null,SysAgents::getId,rateSettingReq.getBelongId())
                .end();

        PageHelper.startPage(rateSettingReq.getPage(),rateSettingReq.getRp());
        Page<RateSettingResp> page = (Page<RateSettingResp>) sysAgentCommissionSettingsMapper.joinSelectList(jlw, RateSettingResp.class);
        //将数据进行处理，给返回类加上上级代理属性和回扣方案
        List<RateSettingResp> rateSettingResps = addAgent(page.getResult());

        return ApiResp.page(page.getPageNum(),page.getPageSize(),(int)page.getTotal(),rateSettingResps);
    }

    /**
     * 新增设定
     * @param rateSetting
     * @return
     */
    @Override
    public ApiResp<SysAgentCommissionSettings> createRateSetting(RateSettingCreateReq rateSetting) {
        SysAgentCommissionSettings settings = new SysAgentCommissionSettings();
        BeanUtils.copyProperties(rateSetting,settings);
        sysAgentCommissionSettingsMapper.insert(settings);
        return ApiResp.sucess(sysAgentCommissionSettingsMapper.selectById(settings.getCsId()));
    }

    //将数据进行处理，给返回类加上上级代理属性和回扣方案
    private List<RateSettingResp> addAgent(List<RateSettingResp> rateSettingResps) {
        List<RateSettingResp> collect = rateSettingResps.stream().map(iter -> {
            SysAgents sysAgents = sysAgentsMapper.selectById(iter.getAgentId());
            //添加上级代理
            if (sysAgents.getBelongId() != null) {
                SysAgents topSysAgents = sysAgentsMapper.selectById(sysAgents.getBelongId());
                iter.setTopAgentId(topSysAgents.getDisplayId());
                iter.setTopAgentName(topSysAgents.getFullName());
            } else {
                iter.setTopAgentId(sysAgents.getDisplayId());
                iter.setTopAgentName(sysAgents.getFullName());
            }
            //添加回扣方案
            //判断其中的四种回扣方案是否有值，如果有值代表有对应的回扣方案信息(1.银行回扣方案 2.QR回扣方案 3.true wallet回扣方案 4.代付回扣方案)
            if (iter.getBdrsRpId() != null) {
                //查询回扣方案表,存在就将回扣方案放入三级返回类中
                SysRebateScheme systemRebateScheme = sysRebateSchemeMapper.selectById(iter.getBdrsRpId());
                if (systemRebateScheme != null) {
                    iter.setBankDepositRebatePlan(systemRebateScheme);
                }
            }
            if (iter.getQdrsRpId() != null) {
                //查询回扣方案表,存在就将回扣方案放入三级返回类中
                SysRebateScheme systemRebateScheme = sysRebateSchemeMapper.selectById(iter.getQdrsRpId());
                if (systemRebateScheme != null) {
                    iter.setQrDepositRebatePlan(systemRebateScheme);
                }
            }
            if (iter.getTwdrsRpId() != null) {
                //查询回扣方案表,存在就将回扣方案放入三级返回类中
                SysRebateScheme systemRebateScheme = sysRebateSchemeMapper.selectById(iter.getTwdrsRpId());
                if (systemRebateScheme != null) {
                    iter.setTrueWalletDepositRebatePlan(systemRebateScheme);
                }
            }
            if (iter.getWrsRpId() != null) {
                //查询回扣方案表,存在就将回扣方案放入三级返回类中
                SysRebateScheme systemRebateScheme = sysRebateSchemeMapper.selectById(iter.getWrsRpId());
                if (systemRebateScheme != null) {
                    iter.setWithdrawRebatePlan(systemRebateScheme);
                }
            }
            return iter;
        }).collect(Collectors.toList());
        return collect;
    }
}